//
//  WuLiHotSearchRequest.m
//  WULIHH
//
//  Created by tang on 16/4/11.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "WuLiHotSearchRequest.h"
#import "Header.h"
#import "UserInfoModel.h"
#import "AFNetworking.h"

@implementation WuLiHotSearchRequest


/*请求明星列表*/
+ (id)requestStarList
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *string = [NSString stringWithFormat:@"%@%@",hostName, getStarListAPI];
    [manager.requestSerializer setValue:[UserInfoModel tokenString] forHTTPHeaderField:@"X-AUTH-TOKEN"];
    [manager.requestSerializer setValue:appVersion forHTTPHeaderField:@"X-APP-VERSON"];
    [manager.requestSerializer setValue:typetext forHTTPHeaderField:@"Content-Type"];

    
    
    [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        // 这里可以获取到目前的数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功，解析数据
        
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        
        
        
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path=[paths objectAtIndex:0];
        NSString *Json_path=[path stringByAppendingPathComponent:@"starList.json"];
        //==写入文件
//        NSLog(@"%@",[responseObject writeToFile:Json_path atomically:YES] ? @"Succeed":@"Failed");
        
        if ([responseObject writeToFile:Json_path atomically:YES])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:getStarList object:@"1" userInfo:nil];
        }
        else
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:getStarList object:@"0" userInfo:nil];
        }
        
        //        NSLog(@"%@", dic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        //        NSLog(@"%@", [error localizedDescription]);
    }];

    return self;
}




+ (id)requestHotRescuse
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *string = [NSString stringWithFormat:@"%@%@",hostName, qingqiuremenziyuanAPI];
    
    
    [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        // 这里可以获取到目前的数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功，解析数据
//        NSLog(@"%@", responseObject);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        [[NSUserDefaults standardUserDefaults] setValue:dic forKey:qingqiuremenziyuan];
//        NSLog(@"%@", dic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
//        NSLog(@"%@", [error localizedDescription]);
    }];
    
    return self;
}




/*搜索输入文字*/
+ (id)searchWithText:(NSString *)string;
{
//    NSLog(@"%@", string);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    [manager.requestSerializer setValue:[UserInfoModel tokenString] forHTTPHeaderField:@"X-AUTH-TOKEN"];
    [manager.requestSerializer setValue:appVersion forHTTPHeaderField:@"X-APP-VERSON"];
    [manager.requestSerializer setValue:typetext forHTTPHeaderField:@"Content-Type"];

    NSString *httpString = [NSString stringWithFormat:@"%@%@%@",hostName, wenziSousuoAPI, [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    
//    NSLog(@"%@", httpString);
    [manager GET:httpString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        // 这里可以获取到目前的数据请求的进度
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功，解析数据
        //        NSLog(@"%@", responseObject);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:wenzisousuo object:nil userInfo:dic];
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        //        NSLog(@"%@", [error localizedDescription]);
    }];

    return self;
}

/*请求token*/
+ (id)getUserToken
{
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    NSString *string = [NSString stringWithFormat:@"%@%@",hostName, qingqiuremenziyuanAPI];
//    
//    
//    [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//        // 这里可以获取到目前的数据请求的进度
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        // 请求成功，解析数据
//        //        NSLog(@"%@", responseObject);
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"%@", dic);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        // 请求失败
//        //        NSLog(@"%@", [error localizedDescription]);
//    }];

    return self;
}


@end
